package leetcode.editor.cn;

import org.junit.Test;

public class Test209 {


    @Test
    public void test() {
        int[] arr = {2, 3, 1, 2, 4, 3};
        minSubArrayLen(7, arr);
    }

    public int minSubArrayLen(int target, int[] nums) {
        int length = nums.length;
        int index = Integer.MAX_VALUE;
        int sum = 0;
        int start = 0;
        int end = 0;
        while (end < length) {
            sum += nums[end];
            while (sum >= target) {
                // 窗口内的总和 减去 start 位置的值
                sum -= nums[start];
                index = Math.min(index, end - start + 1);
                start++;
            }
            end++;
        }
        return index == Integer.MAX_VALUE ? 0 : index;
    }
}